package com.zero.youxiao.controller;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.zero.basic.exception.SmsErrorConstant;
import com.zero.basic.exception.SmsRuntimeException;
import com.zero.basic.exception.SmsStatusCode;
import com.zero.basic.util.JsonCreator;
import com.zero.youxiao.dto.DepDto;
import com.zero.youxiao.service.DepService;

@Controller("depController")
@RequestMapping("dep")
public class DepController {

	@Inject
	private DepService depService;

	@RequestMapping("/searchDeps")
	@ResponseBody
	public Object getDep(Integer depNo, String depName, Integer depType,
			HttpServletResponse response, HttpSession session) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			String depTypeString = "";
			if (-1 != depType) {
				if (0 == depType)
					depTypeString = "公司";
				else if (1 == depType)
					depTypeString = "部门";
			}
			if (depName == null)
				depName = "";
			if (depNo == null)
				depNo = 0;

			ObjectNode depTree = depService.searchDeps(depNo, depName,
					depTypeString);
			objectNode.putPOJO("response", depTree);
			objectNode.put("message", "获取部门列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);

			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

	@RequestMapping("/load")
	@ResponseBody
	public Object loadDep(Integer depId, HttpServletResponse response,
			HttpSession session) {
		System.out.println("load dep info");
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			System.out.println(depId);
			if (depId == null) {
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "部门为空");
			}
			DepDto dep = depService.load(depId);
			objectNode.putPOJO("response", dep);
			objectNode.put("message", "获取部门列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);

			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			e.printStackTrace();
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

	@RequestMapping("/updateDep")
	@ResponseBody
	public Object update(Integer depId, Integer depNo, String depName,
			String depType, String depPhone, String depTax, String depDesc,
			String depCreateDate, HttpServletResponse response,
			HttpSession session) {
		System.out.println(depPhone);
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			if (depId == null)
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,
						SmsErrorConstant.INVALID_AUGUMENT, "部门为空");
			depService.updateDep(depId, depNo, depName, depType, depPhone,
					depTax, depDesc, depCreateDate);
			objectNode.putPOJO("response","");
			objectNode.put("message", "更新部门列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

	@RequestMapping("/addDep")
	@ResponseBody
	public Object addDep(Integer parentDepId,Integer depNo,String depName,
			String depType, String depPhone, String depTax, String depDesc,
			String depCreateDate, HttpServletResponse response,
			HttpSession session) {
		System.out.println(depNo+"_"+depName+"_"+depType);
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			depService.addDep(parentDepId,depNo, depName, depType, depPhone, depTax,
					depDesc, depCreateDate);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "添加部门列表成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

	@RequestMapping("/deleteDep")
	@ResponseBody
	public Object deleteDep(Integer depId, Integer depNo,HttpServletResponse response, HttpSession session) {
		ObjectNode objectNode = JsonCreator.generateJsonObj();
		try {
			if (depId == null)
				throw new SmsRuntimeException(SmsStatusCode.INVALID_CODE,SmsErrorConstant.INVALID_AUGUMENT, "部门为空");
			depService.deleteDep(depId);
			objectNode.putPOJO("response", "");
			objectNode.put("message", "删除部门成功");
			objectNode.put("statusCode", SmsStatusCode.SUC_CODE);
			System.out.println(objectNode);
		} catch (SmsRuntimeException e) {
			objectNode.put("message", e.getErrorDescription());
			objectNode.put("error", e.getError());
			objectNode.put("statusCode", e.getStatusCode());
		} catch (Exception e) {
			e.printStackTrace();
			objectNode.put("message", SmsErrorConstant.SERVER_ERROR_MSG);
			objectNode.put("error", SmsErrorConstant.WEB_APPLICATION);
			objectNode.put("statusCode", SmsStatusCode.SERVER_ERROR);
		}
		return objectNode;
	}

}
